package com.ayf.payment.game.api.utils;

import com.swwx.charm.commons.lang.utils.LogPortal;

import java.math.BigDecimal;

/**
 *
 *
 * @author wwl
 * @date 2020/3/10 11:08
 */
public class MerchantSettleServiceChargeUtil {

    public static BigDecimal getServiceCharge(Integer merchantId, BigDecimal amountMerchant,String feeCustom){
        try {
            if(amountMerchant==null || amountMerchant.compareTo(BigDecimal.ZERO)==0){
                return BigDecimal.ZERO;
            }
            String[] arrayStr = feeCustom.split("\\|");
            for (String str : arrayStr) {
                String[] feeStr = str.split(":");
                BigDecimal amount = new BigDecimal(feeStr[0]);
                BigDecimal fee = new BigDecimal(feeStr[1]);
                if (amountMerchant.compareTo(amount) == 0 || amountMerchant.compareTo(amount) == -1) {
                    return getAmount(amountMerchant, fee);
                }
            }
        }catch (Exception e){
            LogPortal.error("结算金额手续费计算异常！结算商户【{}】",e,merchantId);
        }
        return BigDecimal.ZERO;
    }

    private static BigDecimal getAmount(BigDecimal amountMerchant,BigDecimal fee){
        BigDecimal amount1=BigDecimal.ONE;
        if(fee.compareTo(BigDecimal.ONE)==-1){
            return amountMerchant.multiply(fee).setScale(2,BigDecimal.ROUND_HALF_UP);
        }else{
            return fee;
        }
    }

}
